<?php

namespace addons\sum\controller;

use addons\sum\controller\Common;
use addons\sum\service\Wallet as WalletService;
use app\admin\model\sum\Gift as GiftModel;
use app\admin\model\sum\GiftLog as GiftLogModel;
use app\admin\model\sum\performer\Follow as PerformerFollowModel;
use app\admin\model\sum\performer\Performer as PerformerModel;
use app\admin\model\sum\UserGift as UserGiftModel;
use think\Db;

class Gift extends Common
{

    protected $noNeedLogin = ['index'];
    protected $noNeedRight = ['*'];

    public function index()
    {
        // 使用 PHP 的反射 API 获取 模型 protected 属性
        // 获取多语言的字段
        $obj=new GiftModel();
        $reflection = new \ReflectionClass($obj);
        $property = $reflection->getProperty('lang_field');
        $property->setAccessible(true);
        $lang_field=$property->getValue($obj);
        // 获取当前语言标识
        $langSlug=$this->langSlug;

        $list_rows = $this->request->param('list_rows', 10);
        $records=GiftModel::where('status','normal')->paginate($list_rows);
        $records->each(function ($record)use($lang_field,$langSlug){
            foreach ($lang_field as $lfv){
                $record[$lfv]=$record['lang_data'][$langSlug][$lfv]?$record['lang_data'][$langSlug][$lfv]:$record[$lfv];
            }
            $record->hidden(['image','lang_data']);
        });
        $this->success(__('Success'),$records);
    }

    public function mygift()
    {
        // 使用 PHP 的反射 API 获取 模型 protected 属性
        // 获取多语言的字段
        $obj=new GiftModel();
        $reflection = new \ReflectionClass($obj);
        $property = $reflection->getProperty('lang_field');
        $property->setAccessible(true);
        $lang_field=$property->getValue($obj);
        // 获取当前语言标识
        $langSlug=$this->langSlug;

        $list_rows = $this->request->param('list_rows', 10);
        $user = auth_user();
        $records=UserGiftModel::where('user_id',$user['id'])->where('num','>',0)->paginate($list_rows);
        $records->each(function ($record)use($lang_field,$langSlug){
            $gift=GiftModel::where('id',$record['gift_id'])->find();
            foreach ($lang_field as $lfv){
                $gift[$lfv]=$gift['lang_data'][$langSlug][$lfv]?$gift['lang_data'][$langSlug][$lfv]:$gift[$lfv];
            }
            $gift->hidden(['image','lang_data']);
            $record['gift']=$gift;
        });
        $this->success(__('Success'),$records);
    }

    /**
     * 赠送礼物
     * @return void
     */
    public function give()
    {
        $user = auth_user();
        $performer_id = $this->request->param('performer_id');
        $gift_id = $this->request->param('gift_id');
        $num = $this->request->param('num');
        $type = $this->request->param('type');
        if(!$performer_id){
            $this->error(__('参数错误'));
        }
        if(!$gift_id){
            $this->error(__('参数错误'));
        }
        if(!$num){
            $this->error(__('参数错误'));
        }
        if(!$type){
            $this->error(__('参数错误'));
        }
        $gift_log= Db::transaction(function () use ($num,$user,$type,$gift_id,$performer_id) {
            $gift=GiftModel::where('id',$gift_id)->find();
            $popularity=$gift['popularity']*$num;//人气
            if($type=='buy'){// 购买礼物赠送
                $pay_money=$gift['price']*$num;//价格
                if($pay_money>0){
                    // 扣除用户积分
                    WalletService::change($user, 'score', -$pay_money, 'gift_pay');
                }
            }
            if($type=='self'){// 我的礼物赠送
                $user_gift=UserGiftModel::where('user_id',$user['id'])->where('gift_id',$gift['id'])->find();
                if(!$user_gift){
                    $this->error(__('礼物数量不足'));
                }
                if($num>$user_gift['num']){
                    $this->error(__('礼物数量不足'));
                }
                $user_gift->num=$user_gift->num-$num;
                $user_gift->save();
            }
            //增加演员人气
            $performer=PerformerModel::where('id',$performer_id)->find();
            $performer->popularity=$performer->popularity+$popularity;
            $performer->save();

            //增加自己的贡献人气值
            $follow=PerformerFollowModel::where('performer_id',$performer_id)->where('user_id',$user['id'])->find();
            if($follow){
                $follow->popularity=$follow->popularity+$popularity;
                $follow->save();
            }else{
                $performer->followers=$performer->followers+1;
                $performer->save();
                $follow=new PerformerFollowModel();
                $follow->user_id=$user['id'];
                $follow->user_nickname=$user['nickname'];
                $follow->user_avatar=$user['avatar'];
                $follow->user_gender=$user['gender'];
                $follow->performer_id=$performer_id;
                $follow->popularity=$popularity;
                $follow->save();
            }

            //添加礼物赠送记录
            $gift_log=new GiftLogModel();
            $gift_log->user_id=$user['id'];
            $gift_log->gift_id=$gift_id;
            $gift_log->performer_id=$performer_id;
            $gift_log->save();
            return $gift_log;
        });
        // 触发任务事件-送礼物
        $data = ['event' => 'task_performer_gift', 'user' => $user];
        \think\Hook::listen('task_event', $data);

        // 触发任务事件-榜单前十
        $data2 = ['event' => 'task_performer_rank', 'user' => $user,'performer_id'=>$performer_id];
        \think\Hook::listen('task_event', $data2);

        $this->success(__('Success'));
    }



}
